| ImageGear Java PDF > How to... > Integrate with ImageGear for Java |
As an alternative to using ImageGear Java PDF to open image formats such as JPEG, TIFF, and others (for example, if you want to convert a TIFF or JPEG image to a PDF), you can use the ImageGear for Java SDK if you have it installed. After the image is opened, you can save it as a PDF document using ImageGear Java PDF.
Integrating the ImageGear for Java and ImageGear Java PDF libraries will enable you to do the following:
The following is an illustration of how to open an image and convert it to a BufferedImage:
|
Copy Code | |
|---|---|
import com.accusoft.imagegearpdf.*;
import com.accusoft.imagegear.core.*;
import com.accusoft.imagegear.formats.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
class PdfDemo
{
static
{
// Load ImageGearJavaPDF library.
System.loadLibrary("IgPdf");
// Initialize ImageGear common formats.
ImGearCommonFormats.initialize();
}
// Load image file to a BufferedImage.
public BufferedImage readFileToBufferedImage(String imageFilename)
{
try
{
// Open the image file stream.
ImGearStream imageStream = ImGearStreams.fileStream(imageFilename, "r");
// Load the first page (in case of the multi-page image) of the image file
ImGearPage imagePage = ImGearFileFormats.loadPage(imageStream, 0, null);
// Close the image file stream.
imageStream.close();
// Convert the image page to a BufferedImage.
BufferedImage bufferedImage = ImGearFileFormats.exportToImage(imagePage);
return bufferedImage;
}
catch (Throwable ex)
{
// Failed to load image file.
System.err.println("Exception: " + ex.toString());
return null;
}
}
} | |
An illustration of how to create a new PDF document, add an image from a BufferedImage instance to its first page, and save it out to a PDF file is provided in the topic Add an Image to a PDF.